package com.qingluan.dd.InetTool;

import java.nio.channels.AsynchronousCloseException;
import java.security.PublicKey;

import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.qingluan.arguments.ARGUMENTS;

import android.provider.MediaStore.Video;
import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import de.tavendo.autobahn.WebSocketHandler;



public class GetTalking {
	//json argu area
	public static String talking_status_J		 	=  	"status";
	public static String mission_id_J			 	=  	"mission_id";
	public static String talk_build_email_J		 	=  	"email";
	public static String talk_build_name_J			=	"name";
	public static String talking_name_J			 	=  	"talk_name";
	public static String talking_content_J		 	=  	"talk_content";
	
	public static String oneTalking_talking_id 		= 	"talking_id";
	public static String oneTalking_talking_email 	= 	"talking_email";
	public static String oneTalking_talking_name 	=	"talking_name";
	public static String oneTalking_talking_content = 	"talking_content";
	
	
	public static String talking_J = "talking";
	public static String talk_ids_J = "talk_ids";
	public static String talk_id = "talk_id";
	public static String join_email_J = "join_email";
	//foundation method area
	public static void FillInJson(JSONObject json ,String key,String Value) throws JSONException{
		json.put(key, Value);
		
	}
	
	
	//########################################################################
	/*
	 *  web socket model
	 */
	public static void TalkingWebsocket(String wsUrl,final setOnWebSocket setonget){
		WebSocketConnection client = new WebSocketConnection();
		try {
			client.connect(wsUrl, new WebSocketHandler(){
				@Override
				public void onOpen() {
					// TODO Auto-generated method stub
					setonget.onOpen();
				}
				
				@Override
				public void onClose(int code, String reason) {
					// TODO Auto-generated method stub
					setonget.onClose(reason);
				}
				
				@Override
				public void onTextMessage(String payload) {
					// TODO Auto-generated method stub
					setonget.OnGet(payload);
				}
			});
		} catch (WebSocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public interface setOnWebSocket{
		public void onOpen() ;
		public void onClose(String reason) ;
		public void OnGet(String message);
	}
	
	
	//############################################################################################
	/*
	 *  HTTP post model
	 */
	
	public static void TalkingPost(final setOnPost setOnPost,JSONObject messsagePackage) {
		AsyncHttpClient client = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		
		params.put(ARGUMENTS.POST_GET_TALK, messsagePackage.toString());
		client.post(ARGUMENTS.GET_TALK_IP, params,new AsyncHttpResponseHandler(){
			
			@Override
			public void onSuccess( String json) {
				// TODO Auto-generated method stub
				
				setOnPost.OnPostSuccess(json);
			}
			
			@Override
			public void onFailure(int statusCode, Throwable error,
					String content) {
				// TODO Auto-generated method stub
				setOnPost.OnPostFailed(error.toString());
			}
		});
	}
	
	public static void MessagePullByPost(final setOnPost  setOnPost,JSONObject arguments){
		AsyncHttpClient client = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		
		params.put(ARGUMENTS.POST_PULL_TALKING, arguments.toString());
		client.post(ARGUMENTS.PULL_TALKING_IP, params,new AsyncHttpResponseHandler(){
			
			@Override
			public void onSuccess( String json) {
				// TODO Auto-generated method stub
				
				setOnPost.OnPostSuccess(json);
			}
			
			@Override
			public void onFailure(int statusCode, Throwable error,
					String content) {
				// TODO Auto-generated method stub
				setOnPost.OnPostFailed(error.toString());
			}
		});
	}
	public interface setOnPost{
		public void OnPostSuccess(String json);
		public void OnPostFailed(String error);
		 
	}
	
	
	/*
	 * 
	 * parameter take 
	 */
	
	
}
